package com.b2b.controller;

import com.b2b.model.entity.Contract;
import com.b2b.model.receive.InfoDto;
import com.b2b.util.ContractUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.b2b.exception.CustomException;
import com.b2b.exception.CustomUnauthorizedException;
import com.b2b.model.ContractDto;
import com.b2b.model.common.BaseDto;
import com.b2b.model.common.ResponseBean;
import com.b2b.service.ContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/contract")
@PropertySource("classpath:config.properties")
public class ContractController {
    @Value("${refreshTokenExpireTime}")
    private String refreshTokenExpireTime;

    private final ContractService contractService;
    private final ContractUtil contractUtil;

    @Autowired
    public ContractController(ContractService contractService,ContractUtil contractUtil){
        this.contractService = contractService;
        this.contractUtil = contractUtil;
    }


    @PostMapping("/add")
    public ResponseBean addWorkOrder(@RequestBody ContractDto contractDto) {
        if (contractDto.getFileUrl() == null){
            throw new CustomException("文件路径为空(Query Failure)");
        }
        Contract contractTemp = new Contract();
        contractTemp.setFileUrl(contractDto.getFileUrl());
        Integer count = contractService.selectCount(contractTemp);
        if (count >0){
            throw new CustomException("文件已存在(Query Failure)");
        }
        char state = '1';
        contractDto.setVisibility(state);
        count = contractUtil.addContract(contractDto);
        if (count <= 0 ){
            throw new CustomException("新增失败(Insert Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "新增成功(Insert Success)", contractDto);
    }

//    @GetMapping
//    public ResponseBean contract(BaseDto baseDto){
//        if (baseDto.getPage() == null || baseDto.getRows() == null) {
//            baseDto.setPage(1);
//            baseDto.setRows(10);
//        }
//        PageHelper.startPage(baseDto.getPage(), baseDto.getRows());
//        List<ContractDto> contractDtos = contractService.selectAll();
//        PageInfo<ContractDto> selectPage = new PageInfo<ContractDto>(contractDtos);
//        if (contractDtos == null || contractDtos.size() < 0) {
//            throw new CustomException("查询失败(Query Failure)");
//        }
//        Map<String, Object> result = new HashMap<String, Object>(16);
//        result.put("count", selectPage.getTotal());
//        result.put("data", selectPage.getList());
//        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);
//    }

    @GetMapping("/companyGet")
    public ResponseBean contractByVisibility(InfoDto infoDto){
        if (infoDto.getPage() == null || infoDto.getRows() == null) {
            infoDto.setPage(1);
            infoDto.setRows(10);
        }
        Integer companyId = infoDto.getId();
        PageHelper.startPage(infoDto.getPage(), infoDto.getRows());
        List<Contract> contracts = contractService.selectMyContractByCompanyId(companyId);
        List<ContractDto> contractDtos = new ArrayList<>();
        for (int i = 0; i < contracts.size(); i++) {
            contractDtos.add(contractUtil.getContractDto(contracts.get(i)));
        }
//        List<ContractDto> contractDtos = contractUtil.getContractDtoList(contracts);
        PageInfo<ContractDto> selectPage = new PageInfo<ContractDto>(contractDtos);
        if (contractDtos == null || contractDtos.size() < 0) {
            throw new CustomException("查询失败(Query Failure)");
        }
        Map<String, Object> result = new HashMap<String, Object>(16);
        result.put("count", selectPage.getTotal());
        result.put("data", selectPage.getList());
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);
    }


//    @GetMapping("/byCompany")
//    public ResponseBean contractById(@Validated BaseDto baseDto,@PathVariable("id") Integer id){
//        if (baseDto.getPage() == null || baseDto.getRows() == null) {
//            baseDto.setPage(1);
//            baseDto.setRows(10);
//        }
//        PageHelper.startPage(baseDto.getPage(), baseDto.getRows());
//        ContractDto contractDto = new ContractDto();
//        contractDto.setCompanyId(id);
//        List<ContractDto> contractDtos = contractService.select(contractDto);
//        PageInfo<ContractDto> selectPage = new PageInfo<ContractDto>(contractDtos);
//        if (contractDtos == null || contractDtos.size() < 0) {
//            throw new CustomException("查询失败(Query Failure)");
//        }
//        Map<String, Object> result = new HashMap<String, Object>(16);
//        result.put("count", selectPage.getTotal());
//        result.put("data", selectPage.getList());
//        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);
//    }

    @PutMapping
    //@RequiresPermissions(logical = Logical.AND, value = {"user:edit"})
    public ResponseBean update( @RequestBody ContractDto contractDto) {
        Contract contractTemp = new Contract();
        contractTemp.setContractId(contractDto.getContractId());
        Integer count = contractService.selectCount(contractTemp);
        if (count <= 0) {
            throw new CustomUnauthorizedException("该模板不存在(Account not exist.)");
        }
        count = contractService.updateByPrimaryKeySelective(contractUtil.getContract(contractDto));

        if (count <= 0) {
            throw new CustomException("更新失败(Update Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "更新成功(Update Success)", contractDto);
    }

    @DeleteMapping
    public ResponseBean delete(@RequestBody ContractDto contractDto){
        Contract contractTemp = new Contract();
        contractTemp.setContractId(contractDto.getContractId());
        Integer count = contractService.selectCount(contractTemp);
        if (count <= 0) {
            throw new CustomUnauthorizedException("该模板不存在(Account not exist.)");
        }
        count = contractService.deleteByPrimaryKey(contractDto.getContractId());
        if (count <= 0) {
            throw new CustomException("删除失败(Update Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "删除成功(Delete Success)",null);
    }
}
